
#ifndef _TARM_I2C_H_
#define _TARM_I2C_H_



#include "at32f4xx.h"
#include "my_sys.h"

#define I2C_SPEED                    1000
#define I2C_SLAVE_ADDRESS7           0xD0
#define I2C_EEAddress                0xD0

#define I2C_PORT                     I2C1
#define I2C_CLK                      RCC_APB1PERIPH_I2C1
#define I2C_SCL_PIN                  GPIO_Pins_8                  /* PB.06 */
#define I2C_SCL_GPIO_PORT            GPIOB                       /* GPIOB */
#define I2C_SCL_GPIO_CLK             RCC_APB2PERIPH_GPIOB
#define I2C_SDA_PIN                  GPIO_Pins_9                  /* PB.07 */
#define I2C_SDA_GPIO_PORT            GPIOB                       /* GPIOB */
#define I2C_SDA_GPIO_CLK             RCC_APB2PERIPH_GPIOB

#define I2C_EVT_CHECK_NONE                   ((uint32_t)0x00000000)
#define I2C_EVT_CHECK_ACKFAIL                ((uint32_t)0x00000001)
#define I2C_EVT_CHECK_STOP                   ((uint32_t)0x00000002)

#define I2C_TIMEOUT_FLAG          ((uint32_t)35)     /* Timeout 35 ms */
#define I2C_TIMEOUT_ADDR_SLAVE    ((uint32_t)10000)  /* Timeout 10 s  */
#define I2C_TIMEOUT_BUSY_FLAG     ((uint32_t)10000)  /* Timeout 10 s  */


typedef enum
{
  I2C_OK             = 0,

  I2C_ERROR_STEP_1   = 1,
  I2C_ERROR_STEP_2   = 2,
  I2C_ERROR_STEP_3   = 3,
  I2C_ERROR_STEP_4   = 4,
  I2C_ERROR_STEP_5   = 5,
  I2C_ERROR_STEP_6   = 6,
  I2C_ERROR_STEP_7   = 7,
  I2C_ERROR_STEP_8   = 8,
  I2C_ERROR_STEP_9   = 9,
  I2C_ERROR_STEP_10  = 10,
  I2C_ERROR_STEP_11  = 11,
  I2C_ERROR_STEP_12  = 12,

  I2C_BUSY           = 100,
  I2C_TIMEOUT        = 101,
  I2C_ERROR          = 102,
} I2C_StatusType;


void I2C1_Init();
void LowLevel_Init(void);
I2C_StatusType I2C_Master_Transmit(I2C_Type* I2Cx, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout);
I2C_StatusType I2C_Master_Rec(I2C_Type* I2Cx, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout);

int IIC_readArray(uint8_t i_device7byteAddress, uint8_t i_dateAddr, uint8_t *o_masterTxBuff, uint32_t i_dateNum);
int IIC_sendArray(uint8_t i_device7byteAddress, uint8_t i_dateAddr, const void *i_masterTxBuff, uint32_t i_dateNum);
int IIC_sendOneByte(uint8_t i_device7byteAddress, uint8_t i_dateAddr, uint8_t i_data);
uint8_t IIC_ReadOneByte(uint8_t i_IDAddr7bit, uint8_t i_ReadAddr);

#endif
